package com.xunua.NaoZiKongBai.RegisterActivity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xunua.NaoZiKongBai.Base.BaseActivity;
import com.xunua.NaoZiKongBai.Bean.EventBus.Time60Event;
import com.xunua.NaoZiKongBai.R;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;


public class RegisterActivity extends BaseActivity<RegisterPresenter, RegisterContract.VP> implements View.OnClickListener {

    @BindView(R.id.register_statusBar)
    View registerStatusBar;
    @BindView(R.id.register_img_icon)
    ImageView registerImgIcon;
    @BindView(R.id.register_et_phone)
    EditText registerEtPhone;
    @BindView(R.id.register_rel_name)
    RelativeLayout registerRelName;
    @BindView(R.id.register_getyzm)
    TextView registerGetyzm;
    @BindView(R.id.register_yzm)
    EditText registerYzm;
    @BindView(R.id.register_rel_yzm)
    RelativeLayout registerRelYzm;
    @BindView(R.id.register_et_pwd)
    EditText registerEtPwd;
    @BindView(R.id.register_rel_pwd)
    RelativeLayout registerRelPwd;
    @BindView(R.id.register_rel_ets)
    RelativeLayout registerRelEts;
    @BindView(R.id.register_btn_register)
    Button registerBtnRegister;

    private String registerphone="";
    private String registerpwd="";

    @Override
    public RegisterContract.VP getContract() {
        return new RegisterContract.VP() {
            @Override
            public void request_register(String phone, String pwd,String yzm) {
                showLoadingDialogBase(RegisterActivity.this,"注册中...",false);
                registerphone=phone;
                registerpwd=pwd;
                mPresenter.getContract().request_register(phone, pwd,yzm);
            }

            @Override
            public void response_register(boolean isRegister, String errStr) {
                hideLoadingDialogBase();
                if (isRegister) {
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    //注册成功
                    Intent intent = new Intent();
                    intent.putExtra("phone",registerphone);
                    intent.putExtra("pwd",registerpwd);
                    setResult(RESULT_OK,intent);
                    finish();
                }else {
                    Toast.makeText(RegisterActivity.this, ""+errStr, Toast.LENGTH_SHORT).show();
                }
            }
        };
    }

    @Override
    public void setContentViewId() {
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
    }

    @Override
    public void init() {
        registerGetyzm.setOnClickListener(this);
        registerBtnRegister.setOnClickListener(this);
    }

    @Override
    public RegisterPresenter getPresenterInstance() {
        return new RegisterPresenter();
    }

    @Override
    public boolean IskeyboardEnable() {
        return false;
    }

    @Override
    public View getStatusBar() {
        return registerStatusBar;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register_getyzm:
                /**
                 * TODO template 如果是自定义短信模板，此处替换为你在控制台设置的自定义短信模板名称；如果没有对应的自定义短信模板，则使用默认短信模板。
                 * 一天一个应用给同一手机号发送的短信不能超过10条，否则会报10010错误 。
                 * 短信发送限制规则是1/分钟，5/小时，10/天。
                 */
                BmobSMS.requestSMSCode(registerEtPhone.getText().toString().trim(), "默认短信模板", new QueryListener<Integer>() {
                    @Override
                    public void done(Integer smsId, BmobException e) {
                        if (e == null) {
                            //开始计时60秒
                            myApplication.time60();
                            registerGetyzm.setTextColor(getResources().getColor(R.color.yzm_tv_text_color_unclick));
                            registerGetyzm.setClickable(false);
                            Toast.makeText(RegisterActivity.this, "发送验证码成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(RegisterActivity.this, "发送验证码失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
            case R.id.register_btn_register:
                String phone = registerEtPhone.getText().toString().trim();
                String pwd = registerEtPwd.getText().toString().trim();
                String yzm = registerYzm.getText().toString().trim();

                if (yzm.length()>0&&phone.length()>0&&pwd.length()>0){
                    if (phone.length() != 11) {
                        Toast.makeText(this, "请输入十一位手机号", Toast.LENGTH_SHORT).show();
                    }else if (pwd.length()<8){
                        Toast.makeText(this, "请输入八位及以上的密码", Toast.LENGTH_SHORT).show();
                    }else if(yzm.length()!=6){
                        Toast.makeText(this, "请输入与发送给您的6位短信验证码", Toast.LENGTH_SHORT).show();
                    }else {
                        getContract().request_register(phone, pwd, yzm);
                    }
                }else {
                    Toast.makeText(this, "请不要留空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED,sticky = true)
    public void getTime60(Time60Event event){
        if (event.getTime()<=0){//可以发送短信
            registerGetyzm.setText("获取验证码");
            registerGetyzm.setTextColor(getResources().getColor(R.color.yzm_tv_text_color_click));
            registerGetyzm.setClickable(true);
        }else {
            //不可以发送短信
            registerGetyzm.setText(event.getTime()+"s后再试");
        }
    }
}
